<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("buttons.inc.php");
use_unit("components4phpfull/jteventcalendar.inc.php");
use_unit("menus.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Parts_Create extends Page
{
       public $chkPaid = null;
       public $edRef = null;
       public $Label4 = null;
       public $JSRoundDecimal = null;
       public $lblDesc = null;
       public $btnShowAccont = null;
       public $JTDivAccount = null;
       public $JTGroupBox4 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $lblPrice = null;
       public $Label5 = null;
       public $radioInv = null;
       public $edPartNo = null;
       public $cmbCategory = null;
       public $Label3 = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $JTDate = null;
       public $Label2 = null;
       public $edAmt = null;
       public $Label1 = null;
       public $Image1 = null;
       public $hiddenWO = null;
       public $hiddenVeh = null;
       public $cmbPartNo = null;
       public $lblPartNo = null;
       public $cmbAcct = null;
       public $lblAcct = null;
       public $chkWarranty = null;
       public $lblVendor = null;
       public $cmbVendor = null;
       public $memoNotes = null;
       public $edPrice = null;
       public $lblNotes = null;
       public $edQty = null;
       public $lblQty = null;
       public $JTSiteTheme1 = null;


       function cmbPartNoChange($sender, $params)
       {
       $parts_id = $this->cmbPartNo->getItemIndex();

       $query_veh_part = "SELECT * FROM veh_parts_tbl WHERE vehicle_id = '".$this->hiddenVeh->getValue()."' and part_num='".$parts_id."' order by parts_date DESC limit 1";
       $result_veh_part = mysql_query($query_veh_part) or die("SELECT VEH PART ".mysql_error());
       $row_veh_part = mysql_fetch_array($result_veh_part);

       $part_date = $row_veh_part['parts_date'];

       //die($query_veh_part);

       $result_stat = mysql_query("SELECT * FROM invstocks_status_tbl WHERE stock_id='".$parts_id."'")
       or die("SELECT STAT ".mysql_error());
       $row_stat = mysql_fetch_array($result_stat);

       $span_num = $row_stat['span_num'];
       $span_unit = $row_stat['span_unit'];

       //die($span_num);

       if($span_unit=='months')
       {
       $date_computed = strtotime(date("Y-m-d", strtotime($part_date)) . "+".$span_num." month");
       }

       if($span_unit=='years')
       {
       $date_computed = strtotime(date("Y-m-d", strtotime($part_date)) . "+".$span_num." year");
       }

       $due_date = date("Y-m-d", $date_computed);
       $date_today = date('Y-m-d');
       //die($date_today.$due_date);

       if($date_today < $due_date)
       {
       echo '<script language=javascript>
       alert("same part used not reached maximum life span!");
       </script>';
       }


       }

       function Vehicle_Parts_CreateJSLoad($sender, $params)
       {

       ?>
       //Add your javascript code here
       var r0=document.getElementById('radioInv_0').checked;
       var r1=document.getElementById('radioInv_1').checked;
       var lblpart=document.getElementById('lblPartNo_outer');
       var cmbpart=document.getElementById('cmbPartNo_outer')
       var lbldesc=document.getElementById('lblDesc_outer');
       var edpart=document.getElementById('edPartNo_outer');

       if(r0==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='visible';
       document.getElementById('cmbPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       document.getElementById('lblDesc_outer').style.visibility='hidden';
       document.getElementById('edPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '296px';
       cmbpart.style.left = '347px';
       lbldesc.style.left = '556px';
       edpart.style.left = '627px';
       }

       if(r1==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='hidden';
       document.getElementById('lblDesc_outer').style.visibility='visible';
       document.getElementById('edPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '556px';
       cmbpart.style.left = '627px';
       lbldesc.style.left = '276px';
       edpart.style.left = '347px';
       }
       <?php

       }


       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }

       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.getElementById('edAccount').value = "";
       <?php

       }

       function btnShowAccontJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.getElementBYId('edAccount').value = "";
       <?php

       }

       function Button1JSClick($sender, $params)
       {

       ?>
       var echo0 = document.getElementById('radioInv_0').checked;
       var echo1 = document.getElementById('radioInv_1').checked;
       <?php

       }

       function radioInvJSChange($sender, $params)
       {
       //echo $this->radioInv->ajaxCall("processInv");
       ?>
       var r0=document.getElementById('radioInv_0').checked;
       var r1=document.getElementById('radioInv_1').checked;
       var lblpart=document.getElementById('lblPartNo_outer');
       var cmbpart=document.getElementById('cmbPartNo_outer')
       var lbldesc=document.getElementById('lblDesc_outer');
       var edpart=document.getElementById('edPartNo_outer');

       if(r0==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='visible';
       document.getElementById('cmbPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       document.getElementById('lblDesc_outer').style.visibility='hidden';
       document.getElementById('edPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '296px';
       cmbpart.style.left = '347px';
       lbldesc.style.left = '556px';
       edpart.style.left = '627px';
       }

       if(r1==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='hidden';
       document.getElementById('lblDesc_outer').style.visibility='visible';
       document.getElementById('edPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '556px';
       cmbpart.style.left = '627px';
       lbldesc.style.left = '276px';
       edpart.style.left = '347px';
       }
       <?php

       }

       function btnCancelJSClick($sender, $params)
       {

       ?>
       window.close();
       <?php

       }


       function edPriceJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Parts_Create.edAmt.value = roundNumber((document.Vehicle_Parts_Create.edQty.value) * (document.Vehicle_Parts_Create.edPrice.value),2);


       //accept only numbers
       var numero = document.getElementById("edPrice").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrice").value = numero;

       <?php

       }

       function edQtyJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Parts_Create.edAmt.value = roundNumber((document.Vehicle_Parts_Create.edQty.value) * (document.Vehicle_Parts_Create.edPrice.value),2);
       <?php

       }


       function btnSubmitClick($sender, $params)
       {
       $query_inv = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
       $result_inv = mysql_query($query_inv) or die('INVENTORY - QUANTITY '.mysql_error());
       $row_inv = mysql_fetch_array($result_inv);
       $curr_qty = $row_inv['qty_available'];
       $qty_use = $this->edQty->Text;

       $part_from = $this->radioInv->getItemIndex();

       if($curr_qty>=$qty_use || $part_from==1)
       {
       //INSERT PARTS
       if($this->chkWarranty->Checked==true)
       {
       $warranty='1';
       }
       else
       {
       $warranty='0';
       }

       $parts_id = $this->cmbPartNo->getItemIndex();
       $result_parts = mysql_query("SELECT * FROM invstocks_tbl WHERE stock_id='".$parts_id."'")
       or die("SELECT PARTS ".mysql_error());
       $row_parts = mysql_fetch_array($result_parts);

       $stock_num = $row_parts['stock_num'];
       $stock_desc = $row_parts['description'];



       mysql_query("INSERT INTO veh_parts_tbl(company_id,
       workorder,
       vehicle_id,
       vendor,
       parts_date,
       quantity,
       part_from,
       part_num,
       other,
       reference_num,
       part_desc,
       category,
       price_ea,
       amount,
       account,
       notes,
       warranty)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->hiddenWO->getValue()."',
       '".$this->hiddenVeh->getValue()."',
       '".$this->cmbVendor->getItemIndex()."',
       '".$this->JTDate->Text."',
       '".$this->edQty->Text."',
       '".$this->radioInv->getItemIndex()."',
       '".$this->cmbPartNo->getItemIndex()."',
       '".$this->edPartNo->Text."',
       '".$this->edRef->Text."',
       '".$stock_num."-".$stock_desc."',
       '".$this->cmbCategory->getItemIndex()."',
       '".$this->edPrice->Text."',
       '".$this->edAmt->Text."',
       '".$this->cmbAcct->getItemIndex()."',
       '".$this->memoNotes->Text."',
       '".$warranty."')") or die("INSERT PARTS - ".mysql_error());

        $part_from = $this->radioInv->getItemIndex();
        if($part_from==0)
        {
        //UPDATE QTY
        $new_qty = $curr_qty - $qty_use;
        $query_up_inv = "UPDATE invstocks_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
        mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());

        $query_select_inv_stat = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
        $result_select_inv_stat = mysql_query($query_select_inv_stat) or die("SELECT INV STAT ".mysql_error());
        $count_inv_stat = mysql_num_rows($result_select_inv_stat);

        if($count_inv_stat>0)
        {
         $query_up_inv = "UPDATE invstocks_status_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
         mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
        }
        }

        /*echo '<script language=javascript>
        window.opener.setWODetails();
        window.opener.JTFrameParts.location.reload();
        window.location.href="veh_parts_create.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue().'";
        alert("Successfully Added Record!");
        window.close();
        </script>';*/

        echo "<script type='text/javascript'>
        alert('Successfully added record!');
        window.opener.setWODetails();
        window.opener.JTFrameParts.location.reload();
        var answer = confirm('Add another record?');
        if(answer)
        {
        window.location.href='veh_parts_create.php?veh=".$this->hiddenVeh->getValue()."&WO=".$this->hiddenWO->getValue()."';
        }
        else
        {
        window.close();
        }
        </script>";
       }
       $part_from = $this->radioInv->getItemIndex();
       if($curr_qty<$qty_use && $part_from==0)
       {
        echo '<script language=javascript>
        alert("Insufficient stock, you only have '.$curr_qty.' left.");
        </script>';
       }


       else
       {
        /*echo '<script language=javascript>
        window.opener.setWODetails();
        window.opener.JTFrameParts.location.reload();
        window.location.href="veh_parts_create.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue().'";
        alert("Successfully Added Record!");
        window.close();
        </script>';*/

        echo "<script type='text/javascript'>
        alert('Successfully added record!');
        window.opener.setWODetails();
        window.opener.JTFrameParts.location.reload();
        var answer = confirm('Click OK to add another record or CANCEL to go back to list');
        if(answer)
        {
        window.location.href='veh_parts_create.php?veh=".$this->hiddenVeh->getValue()."&WO=".$this->hiddenWO->getValue()."';
        }
        else
        {
        window.close();
        }
        </script>";
       }
       //redirect('veh_parts_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());
       }


       function btnCancelClick($sender, $params)
       {
       redirect('veh_parts_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());
       }


       function processInv()
       {
       $company_id = $_SESSION['company_id'];
       //type  0 = employee  1 = vendor
       //$this->cmbDismountWorkby->Clear();
       //$this->cmbDismountWorkby->AddItem('', null,'');

       $type = $this->radioInv->getItemIndex();
       if($type==0)
       {
       $this->edPartNo->Enabled=false;
       $this->edPartNo->Color=gray;
       $this->edPartNo->Text="";
       $this->cmbPartNo->Enabled=true;
       $this->cmbPartNo->Clear();
       $result_part = mysql_query("select * from invstocks_tbl WHERE company_id='".$_SESSION['company_id']."'");

       while($row_part = mysql_fetch_array($result_part))
       {
            $this->cmbPartNo->AddItem($row_part['stock_num'].' - '.$row_part['description'], null, $row_part['stock_id']);
       }
       }
       if($type==1)
       {
       $this->edPartNo->Enabled=true;
       $this->edPartNo->Color=white;
       $this->cmbPartNo->Enabled=false;
       //$this->cmbPartNo->Visible=false;
       }
       }

       function max()
       {
       $parts_id = $this->cmbPartNo->getItemIndex();

       $query_veh_part = "SELECT * FROM veh_parts_tbl WHERE vehicle_id = '".$this->hiddenVeh->getValue()."' and part_num='".$parts_id."' order by parts_date DESC limit 1";
       $result_veh_part = mysql_query($query_veh_part) or die("SELECT VEH PART ".mysql_error());
       $row_veh_part = mysql_fetch_array($result_veh_part);

       $part_date = $row_veh_part['parts_date'];

       //die($part_date);

       $result_stat = mysql_query("SELECT * FROM invstocks_status_tbl WHERE stock_id='".$parts_id."'")
       or die("SELECT STAT ".mysql_error());
       $row_stat = mysql_fetch_array($result_stat);

       $span_num = $row_stat['span_num'];
       $span_unit = $row_stat['span_unit'];

       //die($span_num);

       if($span_unit=='months')
       {
       $date_computed = strtotime(date("Y-m-d", strtotime($part_date)) . "+".$span_num." month");
       }

       if($span_unit=='years')
       {
       $date_computed = strtotime(date("Y-m-d", strtotime($part_date)) . "+".$span_num." year");
       }

       $due_date = date("Y-m-d", $date_computed);
       $date_today = date('Y-m-d');
       //die($date_today.$due_date);

       if($date_today >= $due_date)
       {
       echo '<script language=javascript>
       alert("ok");
       </script>';
       }
       if($date_today < $due_date)
       {
       echo '<script language=javascript>
       alert("part used not reached max life span!");
       </script>';
       }
       }





       function Vehicle_Parts_CreateCreate($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $veh_id = $_GET['veh'];
       $WO = $_GET['WO'];
       if($veh_id!=null || $veh_id!='')
       $this->hiddenVeh->setValue($veh_id);
       $this->hiddenWO->setValue($WO);

       $this->cmbPartNo->Clear();
       $this->cmbPartNo->AddItem('--Select--',null,'--Select--');
       $result_part = mysql_query("select * from invstocks_tbl WHERE company_id='".$_SESSION['company_id']."'");

       while($row_part = mysql_fetch_array($result_part))
       {
            $this->cmbPartNo->AddItem($row_part['stock_num'].' - '.$row_part['description'], null, $row_part['stock_id']);
       }
       //$this->cmbPartNo->AddItem('Others', null, 'Others');


       $this->cmbVendor->Clear();
       $this->cmbVendor->AddItem('--Select--',null,'--Select--');
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbVendor->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       $this->cmbVendor->AddItem('Others', null, 'Others');

       $this->cmbAcct->Clear();
        $result_account = mysql_query("select value from lookup_expense_tbl where company_id='".$_SESSION['company_id']."' AND category='accounts'");

        while($row_account = mysql_fetch_array($result_account))
        {
            $this->cmbAcct->AddItem($row_account['value'], null, $row_account['value']);
        }

        //list account
       $this->listAccount->Clear();
        $result_ac = mysql_query("select value from lookup_expense_tbl where company_id='".$_SESSION['company_id']."' AND category='accounts'");

        while($row_ac = mysql_fetch_array($result_ac))
        {
            $this->listAccount->AddItem($row_ac['value'], null, $row_ac['value']);
        }

       $this->JTDate->setText(date('Y-m-d'));
       $this->edQty->Text="1";
       $this->edPrice->Text="";
       $this->edAmt->Text="";
       $this->edRef->Text="";
       $this->memoNotes->Text="&nbsp";
       $this->edPartNo->Text="";
       $this->radioInv->ItemIndex=0;
       $this->cmbPartNo->Enabled=true;
       $this->cmbPartNo->setItemIndex('--Select--');
       //$this->edPartNo->Enabled=false;
       }

}

global $application;

global $Vehicle_Parts_Create;

//Creates the form
$Vehicle_Parts_Create=new Vehicle_Parts_Create($application);

//Read from resource file
$Vehicle_Parts_Create->loadResource(__FILE__);

//Shows the form
$Vehicle_Parts_Create->show();

?>